import requests
import json
from wxauto import WeChat
import os
import qianfan

# 通过环境变量初始化认证信息
# 方式一：【推荐】使用安全认证AK/SK
# 替换下列示例中参数，安全认证Access Key替换your_iam_ak，Secret Key替换your_iam_sk，如何获取请查看https://cloud.baidu.com/doc/Reference/s/9jwvz2egb
#os.environ["QIANFAN_ACCESS_KEY"] = "RbzPkAOBHcExBPGOJdLfsNG5"
#os.environ["QIANFAN_SECRET_KEY"] = "cu4u9wo6R93jXeCyFVkKgpQRy1pFDlvy"

# 方式二：【不推荐】使用应用AK/SK鉴权
# 替换下列示例中参数，将应用API_Key、应用Secret key值替换为真实值
os.environ["QIANFAN_AK"] = "RbzPkAOBHcExBPGOJdLfsNG5"
os.environ["QIANFAN_SK"] = "cu4u9wo6R93jXeCyFVkKgpQRy1pFDlvy"

chat_comp = qianfan.ChatCompletion()

def main(wx, msg):
    # 指定特定模型
    resp = chat_comp.do(model="ERNIE-3.5-8K", messages=[{
        "role": "user",
        "content": msg
    }])

    a = resp["body"]
    text_content = a.get("result")
    wx.SendMsg(msg=text_content + "--此内容为AI生成", who="机器人测试")  # who="你要发送的人"

if __name__ == '__main__':
    wx = WeChat()
    while True:
        msgs = wx.GetAllMessage()
        if msgs:
            if msgs[-1].type == "friend":
                main(wx, msgs[-1].content)
